package com.zxe.freshserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxe.commonserver.constant.MessageConstant;
import com.zxe.commonserver.constant.StatusConstant;
import com.zxe.commonserver.exception.DeletionNotAllowedException;
import com.zxe.commonserver.result.PageResult;
import com.zxe.freshserver.mapper.FreshMapper;
import com.zxe.freshserver.service.FreshService;
import com.zxe.freshserver.pojo.Fresh;
import com.zxe.freshserver.pojo.FreshDTO;
import com.zxe.freshserver.pojo.FreshPageQueryDTO;
import com.zxe.freshserver.pojo.FreshVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class FreshServiceImpl implements FreshService {

    @Autowired
    private FreshMapper freshMapper;

    /**
     * 新增生鲜和对应的口味
     * @param freshDTO
     */
    @Transactional
    public void insert(FreshDTO freshDTO) {

        Fresh fresh = new Fresh();
        BeanUtils.copyProperties(freshDTO, fresh);
        //向生鲜表插入1条数据
        freshMapper.insert(fresh);
    }

    /**
     * 生鲜分页查询
     * @param freshPageQueryDTO
     * @return
     */
    public PageResult pageQuery(FreshPageQueryDTO freshPageQueryDTO) {
        PageHelper.startPage(freshPageQueryDTO.getPage(), freshPageQueryDTO.getPageSize());
        Page<FreshVO> page = freshMapper.pageQuery(freshPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 生鲜批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            Fresh fresh = freshMapper.getById(id);
            if (fresh.getStatus() == StatusConstant.ENABLE) {
                //1.当前生鲜处于起售中不能删除
                throw new DeletionNotAllowedException(MessageConstant.FRESH_ON_SALE);
            }
        }

        //3.删除生鲜表中的生鲜数据
        for (Long id : ids) {
            freshMapper.deleteById(id);
        }
    }

    /**
     * 根据id查询生鲜
     * @param id
     * @return
     */
    public FreshVO findById(Long id) {

        //根据id查询生鲜数据
        Fresh fresh = freshMapper.getById(id);
        //将查询到的数据封装到FreshVO中
        FreshVO freshVO = new FreshVO();
        BeanUtils.copyProperties(fresh, freshVO);
        return freshVO;
    }

    /**
     * 修改生鲜
     * @param freshDTO
     */
    @Transactional
    public void update(FreshDTO freshDTO) {
        //修改生鲜数据
        Fresh fresh = new Fresh();
        BeanUtils.copyProperties(freshDTO, fresh);
        freshMapper.update(fresh);
    }

    /**
     * 起售、停售生鲜
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Fresh fresh = Fresh.builder()
                .status(status)
                .id(id)
                .build();
        freshMapper.update(fresh);
    }

    /**
     * 根据分类id查询生鲜
     * @param categoryId
     * @return
     */
    public List<FreshVO> findByCategoryId(Long categoryId) {
        List<FreshVO> freshes = freshMapper.findByCategoryId(categoryId);
        return freshes;
    }

    /**
     * 根据id查询分类是否关联具体生鲜
     * @param categoryId
     * @return
     */
    public Integer countByCategoryId(Long categoryId) {
        Integer count = freshMapper.countByCategoryId(categoryId);
        return count;
    }
}

